<template>
  <section>
     <!-- 面包屑 -->
 <breadcrumbs></breadcrumbs>
    <el-form ref="searchForm"
             :model="searchForm"
             label-width="100px"
             inline
             class="demo-form-inline"
             size='small'
             style="border-bottom: 1px solid #f6f6f6;">
      <el-form-item label="商品名称"
                    prop="name">
        <el-input v-model="searchForm.name"
                  placeholder="请输入商品名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="search">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <btns @exec="exec"></btns>
    <div class="data-container scroll-table">
      <div>
        <div class="ind-title lh50 f16">广告位列表</div>
      </div>
      <el-table :data="tableData"
                style="width: 100%"
                row-key="dictId"
                border
                @selection-change="tableSelectionChange">
        <el-table-column type="selection"
                         width="120">
        </el-table-column>
        <el-table-column label="位置">
          <template width="90"
                    slot-scope="scope">
            <img style="width:120px;height:120px;border:none;"
                 :src="scope.row.cover">
          </template>
        </el-table-column>
        <el-table-column prop="name"
                         label="类型">
        </el-table-column>
      </el-table>
    </div>
    <div class="pages">
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="currentPage"
                     :page-sizes="[4, 8, 10, 20]"
                     :page-size="pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </div>
    <el-dialog title="添加广告位信息"
               :visible.sync="dialogCreateFormVisible">
      <el-form :model="createForm"
               label-width="120px"
               ref="createForm">
        <el-form-item label="位置">
          <el-select v-model="createForm.region"
                     placeholder="请选择banner位置">
            <el-option label="首页广告位"
                       value="1"></el-option>
            <el-option label="商城广告位"
                       value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商家广告位">
          <el-upload :action="importFileUrl"
                     list-type="picture-card"
                     class="ad-style"
                     :before-upload="beforeAvatarUpload"
                     :auto-upload="true"
                     :limit="limitNum"
                     :on-success="getUrlAd"
                     :disabled="isChange"
                     :accept="'image/*'"
                     :show-file-list="true">
            <img v-if="isShow"
                 :src="Adaddfile"
                 class="img-feng"
                 alt="广告位图片">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogCreateFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click='addBanner'>确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import btns from '@/components/btns.vue';
import Editor from '@/components/edit.vue';
import breadcrumbs from "@/components/breadcrumbs.vue";
export default {
  components: {
    btns,
    Editor,
    breadcrumbs,
  },
  data () {
    return {
      limitNum: 5,
      isChange: false,
      isShow: false,
      Adaddfile: '',
      searchForm: { name: '', },
      tableData: [],
      currentPage: 1,
      pageSize: 4,
      total: 0,
      dialogCreateFormVisible: false,
      createForm: {},
      formLabelWidth: '120px',
      addfile: '',
      importFileUrl: this.GLOBAL.imgBaseUrl,
      isShowEdit: false
    }
  },
  methods: {
    getUrlAd (item) {
      this.Adaddfile = item.data.url;
      // console.log(item);
      // this.isShow = true
      this.$message({
        type: 'success',
        message: '图片上传成功'
      });
    },
    showEdit () {
      // console.log(this.createForm.type);
      if (this.createForm.type == 1) {
        this.isShowEdit = true;
      } else if (this.createForm.type == 2) {
        this.isShowEdit = false
      } else if (this.createForm.type == 3) {
        this.isShowEdit = false
      }
    },
    addBanner () {
      console.log(this.createForm);
    },
    beforeAvatarUpload (file) {
      console.log(file, 231232)
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 10MB!');
      }
      return isLt2M;
    },
    getUrl (item) {
      let index = 0;
      this.addfile = item.data.url;
      // console.log(item);
      // this.isShow = true;
      this.$message({
        type: 'success',
        message: '图片上传成功'
      });
      // console.log(this.editimg);
      this.editimg = item.data.url
      this.hasfile = true;
      index++;
      if (index == this.limitNum) {
        this.isChange = true;
        this.$message.error('商家广告位最多只能插入五张')
      }
    },
    //打开新增模态框
    add () {
      this.dialogCreateFormVisible = true;
    },
    //打开编辑模态框
    update () { },
    //删除数据
    del () { },
    //启用
    enable () { },
    //禁用
    disable () { },
    search () { },
    handleReset () {
      this.$refs.searchForm.resetFields();
      this.search()
    },
    exec (fun) {
      if (this[fun]) {
        this.funTit = fun;
        this[fun]()
      } else {
        this.$message.error('方法未定义');
      }
    },
    tableSelectionChange (val) {
      this.tableSelection = val;
    },
    handleSizeChange (val) {
      this.getList(this.currentPage, val);
      // eslint-disable-next-line no-console
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange (val) {
      this.getList(val, this.pageSize);
      // eslint-disable-next-line no-console
      // console.log(`当前页: ${val}`);
    },
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
  margin-top: 50px;
}
.img-feng {
  width: 20px;
  height: 20px;
}
.ad-style {
  width: 10px;
}
.el-upload-list--picture-card .el-upload-list__item-actions {
  width: 50%;
}
</style>